package controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.HibernateUtil;
import db.HooldusteenuseTellimus;

import model.Order;

/**
 * Servlet implementation class MainPageController
 */
@WebServlet("/pealeht")
public class MainPageController extends BaseController {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainPageController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void get(HttpServletRequest request, HttpServletResponse response, String username) throws ServletException, IOException {
		RequestDispatcher view = request.getRequestDispatcher("/default.jsp");
		List<HooldusteenuseTellimus> tellimused = HibernateUtil.getTellimused(username); 
		List<Order> orders = new ArrayList<Order>();
		request.setAttribute("tellimused", tellimused);
		for(int i = 0; i<=3;i++){
			Order o = new Order();
			o.setClientId(2365+i);
			o.setOrderNr(100+i);
			o.setSum(new BigDecimal("300").add(new BigDecimal(i*10)));
			o.setDate(new Date());		
			orders.add(o);
		}
		request.setAttribute("orders", orders);
		view.forward(request, response); 
	}

	protected void post(HttpServletRequest request,
		HttpServletResponse response, String username)
		throws ServletException, IOException {
	    // TODO Auto-generated method stub
	    
	}

}
